package main

import (
	"fmt"
	"time"
	"sync"
	"log"
	"os"
)

func main(){
	var wg sync.WaitGroup

	logFile, err := os.Create("trace.log")
	if err != nil {
		log.Fatalln("Fail to create log!")
		return
	}

	logger = log.New(logFile, "", log.Lmicroseconds)

	ticker := time.NewTicker(time.Minute * 1)

	// 程序开始先将所有市场币种拉下来
	PullAllMarkets()

	go func (){ // 定时拉取
		for _ = range ticker.C {
			go PullAllMarkets()
		}
	}()

	for{
		t := time.Now()
		wg.Add(1)
		go CoinEggMarketsToRedis(&wg)

		wg.Add(1)
		go bigOneMarketsToRedis(&wg)

		wg.Add(1)
		go BinanceMarketsToRedis(&wg)

		wg.Add(1)
		go HuobiMarketsToRedis(&wg)

		wg.Add(1)
		go ZbMarketsToRedis(&wg)

		wg.Add(1)
		go OkexMarketsToRedis(&wg)

		wg.Add(1)
		go GateMarketsToRedis(&wg)

		wg.Wait()
		fmt.Printf("Total Duration: %s.\n", time.Now().Sub(t))
		time.Sleep(time.Second * 1)
	}
}

func PullAllMarkets(){
	var wg sync.WaitGroup

	// 此处异步拉取各个市场币种
	wg.Add(1)
	PullBigOneMarkets(&wg)

	//
	wg.Add(1)
	PullHuobiMarkets(&wg)

	wg.Wait()
}
